package controllers

import (
	"blog/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"time"
)

type MenuController struct {
	beego.Controller
}

//列表
func (c*MenuController) List(){
	o := orm.NewOrm()
	var menu []models.Menu
	qs := o.QueryTable(new(models.Menu))
	_, err := qs.All(&menu)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}
	c.Data["json"] = map[string]interface{}{"code":0,"msg":"请求成功","count":0,"data":menu}
	c.ServeJSON()
	return
}

//新增
func (c*MenuController) Add(){
	o := orm.NewOrm()
	name := c.GetString("name")
	url := c.GetString("url")
	sort,_ := c.GetInt("sort")

	var startTime = "2006-01-02 15:04:05"

	nowDate := time.Now().Format("2006-01-02 15:04:05")
	createdAt, _ := time.Parse(startTime,nowDate);

	var menu models.Menu
	menu.Name = name
	menu.Url = url
	menu.Sort = sort
	menu.Created_at = createdAt
	_, err := o.Insert(&menu)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}
	c.Data["json"] = map[string]interface{}{"code":200,"msg":"请求成功","data":""}
	c.ServeJSON()
	return
}

//删除
func (c*MenuController) Delete(){
	o := orm.NewOrm()
	id,_ := c.GetInt("id")
	menu := models.Menu{Id:id}
	_, err := o.Delete(&menu)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}
	c.Data["json"] = map[string]interface{}{"code":200,"msg":"请求成功","data":""}
	c.ServeJSON()
	return
}

